package com.kiln.juxingqs.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.SearchRetAdapter
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.databinding.ActivitySearchretBinding
import com.kiln.juxingqs.ui.vm.SearchRetModel
import com.kiln.juxingqs.BaseMiddleActivity
class SearchRetActivity : BaseMiddleActivity<SearchRetModel, ActivitySearchretBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_searchret
    }

    val goodsAdapter: SearchRetAdapter by lazy {
        SearchRetAdapter(this)
    }


    override fun initView() {

        binding.event = this
        binding.vm = viewModel
        binding.recyclerView.adapter = goodsAdapter
        binding.emptyLayout.setOnClickListener {
            goSearch()
        }
        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.onLoadMore = {
            if (goodsAdapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.getData()
            }
        }

    }

    override fun observerUI() {
        viewModel.ret.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.goods_list as MutableList<GoodsIndex.Goods?>?
                    if (viewModel.page.value == 1) {
                        goodsAdapter.submitList(list)
                    } else
                        goodsAdapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            goodsAdapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (goodsAdapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        goodsAdapter.hasMoreState.value = false
                    }
                } else {
                    if (goodsAdapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

    }

    override fun initViewModel(): SearchRetModel = createViewModel()

    override fun initData() {
        intent.getStringExtra("keyWords")?.let {
            viewModel.wordVal.value = it
            goSearch()
        }
    }

    fun goSearch() {
        if (viewModel.wordVal.value.isNullOrEmpty()) {
            toast("请输入关键字")
            return
        }
        viewModel.page.value = 0
        viewModel.getData()
    }

}